<?php
################################################################################
#             PHP Webcams Script              #  Version 2.0.3  #  06-16-2004  #
################################################################################
#                                                                              #
#    PHP Webcams Script: Displays regularly updated pictures on a website      #
#                                                                              #
#    http://code.google.com/p/phpwebcams                                       #
#                                                                              #
#    Copyright (C) 2003-2004  Randy Becker                                     #
#                                                                              #
#    Feel free to contact me at phpwebcams@lemnar.com                          #
#                                                                              #
################################################################################

################################################################################
#                                   License                                    #
################################################################################
#                                                                              #
#  This program is free software; you can redistribute it and/or modify        #
#  it under the terms of the GNU General Public License as published by        #
#  the Free Software Foundation; either version 2 of the License, or           #
#  (at your option) any later version.                                         #
#                                                                              #
#  This program is distributed in the hope that it will be useful,             #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#  GNU General Public License for more details.                                #
#                                                                              #
#  You should have received a copy of the GNU General Public License           #
#  along with this program; if not, write to the Free Software                 #
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   #
#                                                                              #
################################################################################

################################################################################
#                                   History                                    #
################################################################################
#                                                                              #
#  Pre-1.0: Creation - GPL. Many changes: html to php, single file to many,    #
#    back to single. Started use of xhtml, css, decided on php to generate     #
#    image to account for camera failure, introduction of cam hours (after     #
#    big bandwith usage with 24/7 cams ;) ), descriptive cam img alt tags      #
#    function usage, horizontal allcams style, documentation.                  #
#                                                                              #
#  1.0: First GPL'd version.                                                   #
#                                                                              #
#  1.0.1: Converted while loops to for loops. Used sizeof() to generate        #
#    $number. Removed $thispage variable in favor of $PHP_SELF variable.       #
#                                                                              #
#  1.0.2: Changed documentation format. Added link to horizontal style to      #
#    allcamspage. Made publicly avaibable. Alphabetized functions. Made        #
#    function variables all local. Changed CRs in echo()s to \n s. Made        #
#    one command functions one liners. Changed $n to a much more reasonable    #
#    $i. Alphabetized all variables. Added date and version to top of page.    #
#    Added $credits variable.                                                  #
#                                                                              #
#  2.0.0: Rewrite. Consolidation of variables. Nifty default values for almost #
#    all variables. Added View Source link.                                    #
#                                                                              #
#  2.0.1: Released to soureceforge.net                                         #
#                                                                              #
#  2.0.2: Fixed image quality bug in resize_image with CreateImageTrueColor    #
#    for newer versions of PHP and GD. Should still work with older installs   #
#    also.                                                                     #
#                                                                              #
#  2.0.3: Merged in support for still images (set update=0) and png support    #
#    (thanks red0x!).                                                          #
#                                                                              #
#  2.0.4: More careful variable definition checking                            #
#                                                                              #
################################################################################

################################################################################
#                                 Instructions                                 #
################################################################################
#                                                                              #
#  Fairly simple: set up webcam software to upload a picture named webcamN.jpg #
# where N is the number of the cam. Then, set your variables to the correct    #
# values. The full license can be found in the license.txt file or at          #
# http://www.gnu.org/copyleft/gpl.html                                         #
#                                                                              #
################################################################################

################################################################################
#                             Variable Definitions                             #
################################################################################
#                                                                              #
#  Pretty much all of the variables have sensible defaults, check out the      #
# set_defaults function to see some of them. The only variables you should     #
# need to change are the $camdefs, $prefs['title'], and $prefs['back_link'].   #
# You may also want to add $prefs['page_name'] if the script is named          #
# something other than index.php.                                              #
#                                                                              #
################################################################################

################################################################################
#                              Variable Examples                               #
################################################################################
#                                                                              #
# # camera definitions                                                         #
#                                                                              #
# $camdefs[1]['schedule'] = "6:00 AM - 2:45 PM, Mon - Fri";                    #
# $camdefs[1]['name'] = "Classroom 317";                                       #
# $camdefs[1]['update'] = "30";                                                #
#                                                                              #
# $camdefs[2]['schedule'] = "6:00 AM - 6:00 PM, Everyday";                     #
# $camdefs[2]['name'] = "Outside";                                             #
# $camdefs[2]['update'] = "60";                                                #
#                                                                              #
# $camdefs[3]['schedule'] = "6:00 AM - 5:00 PM, Mon - Fri";                    #
# $camdefs[3]['name'] = "Media Center";                                        #
# $camdefs[3]['update'] = "120";                                               #
#                                                                              #
# # preferences                                                                #
#                                                                              #
# $prefs['title'] = "PHP Webcams";                                             #
# $prefs['back_link'] = "<a href=\"back.html\">Back to PHP Webcams</a>";       #
#                                                                              #
################################################################################


# camera definitions

$camdefs[1]['schedule'] = '6:00 AM - 2:45 PM, Mon - Fri';
$camdefs[1]['name'] = 'Classroom 317';
$camdefs[1]['update'] = '30';

$camdefs[2]['schedule'] = '6:00 AM - 6:00 PM, Everyday';
$camdefs[2]['name'] = 'Outside';
$camdefs[2]['update'] = '60';

$camdefs[3]['schedule'] = '6:00 AM - 5:00 PM, Mon - Fri';
$camdefs[3]['name'] = 'Media Center';
$camdefs[3]['update'] = '120';


# preferences

$prefs['title'] = 'PHP Webcams';
$prefs['back_link'] = '<a href="http://phpwebcams.sourceforge.net/">Back to PHP Webcams</a>';

# main program logic

set_defaults();
if (defined($HTTP_GET_VARS['mode']) && ($HTTP_GET_VARS['mode'] == "view_source")) {
  highlight_file($prefs['page_name']);
} else {
  if (defined($HTTP_GET_VARS['cam'])) {
      $cam = addslashes($HTTP_GET_VARS['cam']);
      $mode = '';
	  if (defined($HTTP_GET_VAR['mode'])) {
		  $mode = addslashes($HTTP_GET_VARS['mode']);
	  }
      if ($cam == "all") {
          if ($mode == "horizontal") {
              all_cams('horizontal');
          } else {
              all_cams();
          }
      } else {
          if ($mode == "thumbnail") {
              display_thumbnail($cam);
          } elseif ($mode == "image") {
              display_image($cam);
          } else {
              cam_page($cam);
          }
      }
  } else {
    main_page();
  }
}


# primary functions

# main_page: the default index of cameras page
function main_page() {
  global $prefs;
  global $camdefs;
  html_header($prefs['title']);
  html_body();
  echo "  <center>\n";
  echo "   <h1>".$prefs['title']."</h1>\n";
  echo "   <a href=\"".$prefs['page_name']."?cam=all\"><hr /></a><br />\n";
  echo "   <table>\n";
  reset($camdefs);
  while (list($cam, $key_array) = each($camdefs)) {
    if (image_is_old($cam)) {$status_text = $camdefs[$cam]['offline_text'];} else {$status_text = $camdefs[$cam]['online_text'];}
    echo "    <tr>\n";
    echo "     <td><a href=\"".$prefs['page_name']."?cam=$cam\"><img alt=\"".$camdefs[$cam]['name']."\" src=\"".$prefs['page_name']."?cam=$cam&amp;mode=thumbnail\" width=\"".$camdefs[$cam]['thumbnail_width']."\" height=\"".$camdefs[$cam]['thumbnail_height']."\" /></a></td>\n";
    echo "     <td style=\"text-align: left;\"><a href=\"".$prefs['page_name']."?cam=$cam\">".$camdefs[$cam]['name']."</a><br />".$camdefs[$cam]['schedule']."<br />$status_text</td>\n";
    echo "    </tr>\n";
  }
  echo "   </table>\n";
  echo "   <br />".$prefs['back_link']."\n";
  echo "   <br /><font size=\"-2\">".$prefs['credits']."</font>\n";
  echo "  </center>\n";
  html_footer();
}

# cam_page: a page showing one camera
function cam_page($cam) {
  global $prefs;
  global $camdefs;
  html_header($prefs['title']." :: ".$camdefs[$cam]['name']);
  html_body($cam);
  echo "  <center>\n";
  echo "   <h1>".$camdefs[$cam]['name']."</h1><br />\n";
  echo "   <img alt=\"".$camdefs[$cam]['name']."\" src=\"".$prefs['page_name']."?cam=$cam&amp;mode=image\" name=\"cam".$cam."picture\" /><br />\n";
  echo "   <br />Image reloads every ".$camdefs[$cam]['update']." seconds.<br />\n";
  echo "   <br /><a href=\"".$prefs['page_name']."\">Back to ".$prefs['title']."</a><br />\n";
  echo "   <font size=\"-2\">".$prefs['credits']."</font>\n";
  echo "  </center>\n";
  html_footer();
}

# all_cams: a page showing all cameras at once
function all_cams($mode="vertical") {
  global $prefs;
  global $camdefs;
  if ($mode == "horizontal") {
    $otr = "<tr>";
    $ostr = "</tr>";
  } else {
    $tr = "<tr>";
    $str = "</tr>";
    $ohl = "   <br /><a href=\"".$prefs['page_name']."?cam=all&amp;mode=horizontal\">---</a><br />\n";
  }
  html_header($prefs['all_cams_title']);
  html_body('all');
  echo "  <center>\n";
  echo "   <table>$otr\n";
  reset($camdefs);
  while (list($cam, $key_array) = each($camdefs)) {
    echo "    $tr<td><a href=\"".$prefs['page_name']."?cam=$cam\"><img alt=\"".$camdefs[$cam]['name']."\" src=\"".$prefs['page_name']."?cam=$cam&amp;mode=image\" name=\"cam".$cam."picture\" /></a></td>$str\n";  
  }
  echo "   $ostr</table>\n";
  echo $ohl;
  echo "   <br /><a href=\"".$prefs['page_name']."\">Back to ".$prefs['title']."</a><br />\n";
  echo "   <font size=\"-2\">".$prefs['credits']."</font>\n";
  echo "  </center>\n";
  html_footer();
}

# display_thumbnail: returns a shrunken image if current, and an offline pic if not.
function display_thumbnail($cam) {
  global $camdefs;
  resize_image($cam, $camdefs[$cam]['thumbnail_width'], $camdefs[$cam]['thumbnail_height']);
}

# display_image: returns an image if current, and an offline pic if not.
function display_image($cam) {
  global $camdefs;
  resize_image($cam, $camdefs[$cam]['width'], $camdefs[$cam]['height']);
}


# supplimentary functions

# html_header: start html page
function html_header($title) {
  echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
  echo " <head>\n";
  echo "  <title>$title</title>\n";
  echo "  <style>\n";
  echo "   span.online {font-weight: bold; color: #009900; text-align: left;}\n";
  echo "   span.offline {font-weight: bold; color: #990000; text-lign: left;}\n";
  echo "   body {font-family: sans-serif;}\n";
  echo "   a {color: #000099;}\n";
  echo "  </style>\n";
}

# javascript: generate javascript for updating
function javascript($cam) {
  global $camdefs;
  echo "  <script language=\"JavaScript\" type=\"text/javascript\">\n";
  if ($cam == "all") {
    $scbody = "   function startClock() {";
    reset($camdefs);
    while (list($cam, $key_array) = each($camdefs)) {
      javascript_basic($cam);
      $scbody .= "startClock".$cam."(); ";
    }
    echo $scbody."}\n";
  } else {
    javascript_basic($cam);
    echo "   function startClock() {startClock".$cam."();}\n";
  }
  echo "  </script>\n";
}

# javascript_basic: echo javascript vars and basic functions
function javascript_basic($cam) {
  global $prefs;
  global $camdefs;
  echo "   var pic".$cam."name = \"".$prefs['page_name']."?cam=$cam&mode=image\";\n";
  echo "   var speed".$cam." = ".$camdefs[$cam]['update']."; var y".$cam." = ".($camdefs[$cam]['update']/2)."; var x".$cam." = speed".$cam."; var cached".$cam." = 0; var cachepic".$cam." = new Image();\n";
  echo "   function recache".$cam."() {var now".$cam." = new Date(); var cam".$cam."Img = pic".$cam."name + \"&utime=\" + now".$cam.".getTime(); cachepic".$cam.".src = cam".$cam."Img; }\n";
  echo "   function reload".$cam."() { document.images.cam".$cam."picture.src = cachepic".$cam.".src; cached".$cam." = 0; x".$cam." = speed".$cam."; }\n";
  echo "   function startClock".$cam."() {if (x".$cam." != \"00\") {x".$cam." = x".$cam." - 1; if (x".$cam." <= y".$cam.") { if (cached".$cam." == 0) recache".$cam."(); cached".$cam." = 1;} if (x".$cam." <= 0) { reload".$cam."() } timerID = setTimeout(\"startClock".$cam."()\", 1000);}}\n";
}

# html_body: end html header, start html body
function html_body($cam="none") {
  global $prefs;
  global $camdefs;
  if ($cam == "all" || $cam == "none") {
    $bgcolor = $prefs['background_color'];
    $background = $prefs['background_image'];
  } else {
    $bgcolor = $camdefs[$cam]['background_color'];
    $background = $camdefs[$cam]['background_image'];
  }
  $body = '';
  if ($cam != "none") {
    javascript($cam);
    $body = "onLoad=\"startClock();\"";
  }
  echo " </head>\n";
  echo " <body bgcolor=\"$bgcolor\" background=\"$background\" $body>\n";
}

# html_footer: end html body, end html page
function html_footer() {
  echo " </body>\n";
  echo "</html>";
}

# image_is_old: returns 1 if too old, 0 if not.
function image_is_old($cam) {
  global $camdefs;
  if ($camdefs[$cam]['update'] == 0) {return 0;}
  if ( filemtime($camdefs[$cam]['filename']) <= ( date("U") - $camdefs[$cam]['old_after'] ) ) {
    return 1;
  } else {
    return 0;
  }
}

# resize_image: output resized image or offline image if too old
function resize_image($cam, $new_width, $new_height) {
  global $camdefs;
  if (image_is_old($cam)) {$camdefs[$cam]['filename'] = $camdefs[$cam]['offline_image'];}
  $path_info = pathinfo($camdeefs[$cam]['filename']);
  if ($path_info['extention'] == 'png') {
    $old_image = ImageCreateFromPng($camdefs[$cam]['filename']);
  } else {
    $old_image = ImageCreateFromJpeg($camdefs[$cam]['filename']);
  }
  $new_image = @ImageCreateTrueColor($new_width,$new_height) or $new_image = ImageCreate($new_width,$new_height);
  $old_size = GetImageSize($camdefs[$cam]['filename']);
  $old_width = $old_size[0];
  $old_height = $old_size[1];
  ImageCopyResized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
  if ($path_info['extention'] == 'png') {
    header("Content-type: image/png");
    ImagePng($new_image);
  } else {
    header("Content-type: image/jpeg");
    ImageJpeg($new_image, null, -1);
  }
  ImageDestroy($old_image);
  ImageDestroy($new_image);
}

# set_defaults: sets any unset variables to their default values
function set_defaults() {
  global $prefs;
  global $camdefs;
  global $HTTP_SERVER_VARS;
  if (!(isset($prefs['version']))) {$prefs['version'] = '2.0.3';}
  if (!(isset($prefs['title']))) {$prefs['title'] = 'Webcams';}
  if (!(isset($prefs['all_cams_title']))) {$prefs['all_cams_title'] = 'All '.$prefs['title'];}
  if (!(isset($prefs['filename_prepend']))) {$prefs['filename_prepend'] = 'webcam';}
  if (!(isset($prefs['filename_postpend']))) {$prefs['filename_postpend'] = '.jpg';}
  if (!(isset($prefs['thumbnail_width']))) {$prefs['thumbnail_width'] = '80';}
  if (!(isset($prefs['thumbnail_height']))) {$prefs['thumbnail_height'] = '60';}
  if (!(isset($prefs['update']))) {$prefs['update'] = '60';}
  if (!(isset($prefs['schedule']))) {$prefs['schedule'] = 'every once in a while';}
  if (!(isset($prefs['old_after']))) {$prefs['old_after'] = '500';}
  if (!(isset($prefs['background_color']))) {$prefs['background_color'] = '#ffffff';}
  if (!(isset($prefs['background_image']))) {$prefs['background_image'] = 'background.jpg';}
  if (!(isset($prefs['offline_image']))) {$prefs['offline_image'] = 'offline.jpg';}
  if (!(isset($prefs['offline_text']))) {$prefs['offline_text'] = "<span class=\"offline\">Sorry, try again later.</span>";}
  if (!(isset($prefs['online_text']))) {$prefs['online_text'] = "<span class=\"online\">Webcam Functioning!</span>";}
  if (!(isset($prefs['page_name']))) {$prefs['page_name'] = "index.php";}
  if (!(isset($prefs['back_link']))) {$prefs['back_link'] = "<a href=\"".$HTTP_SERVER_VARS['HTTP_REFERER']."\">Back</a>";}
# if (!(isset($prefs['credits']))) {$prefs['credits'] = "<a style=\"color: #000000; text-decoration: none;\" href=\"".$prefs['page_name']."?mode=view_source\">Webcams PHP Script </a><a style=\"color: #000000; text-decoration: none;\" href=\"secret.php\">v</a><a style=\"color: #000000; text-decoration: none;\" href=\"".$prefs['page_name']."?mode=view_source\">".$prefs['version']." by Randy</a>";}
  if (!(isset($prefs['credits']))) {$prefs['credits'] = "<a style=\"color: #000000; text-decoration: none;\" href=\"".$prefs['page_name']."?mode=view_source\">Webcams PHP Script v".$prefs['version']." by Randy</a>";}
# if (!(isset($prefs['name']))) {$prefs['name'] = 'Webcam';}
# if (!(isset($prefs['width']))) {$prefs['width'] = '320';}
# if (!(isset($prefs['height']))) {$prefs['height'] = '240';}
# if (!(isset($prefs['filename']))) {$prefs['filename'] = $prefs['filename_prepend'] . $prefs['filename_postpend'];}
  reset($camdefs);
  while (list($cam, $key_array) = each($camdefs)) {
    if (!(isset($camdefs[$cam]['filename_prepend']))) {$camdefs[$cam]['filename_prepend'] = $prefs['filename_prepend'];}
    if (!(isset($camdefs[$cam]['filename_postpend']))) {$camdefs[$cam]['filename_postpend'] = $prefs['filename_postpend'];}
    if (!(isset($camdefs[$cam]['thumbnail_width']))) {$camdefs[$cam]['thumbnail_width'] = $prefs['thumbnail_width'];}
    if (!(isset($camdefs[$cam]['thumbnail_height']))) {$camdefs[$cam]['thumbnail_height'] = $prefs['thumbnail_height'];}
    if (!(isset($camdefs[$cam]['update']))) {$camdefs[$cam]['update'] = $prefs['update'];}
    if (!(isset($camdefs[$cam]['old_after']))) {$camdefs[$cam]['old_after'] = $prefs['old_after'];}
    if (!(isset($camdefs[$cam]['schedule']))) {$camdefs[$cam]['schedule'] = $prefs['schedule'];}
    if (!(isset($camdefs[$cam]['background_color']))) {$camdefs[$cam]['background_color'] = $prefs['background_color'];}
    if (!(isset($camdefs[$cam]['background_image']))) {$camdefs[$cam]['background_image'] = $prefs['background_image'];}
    if (!(isset($camdefs[$cam]['offline_image']))) {$camdefs[$cam]['offline_image'] = $prefs['offline_image'];}
    if (!(isset($camdefs[$cam]['offline_text']))) {$camdefs[$cam]['offline_text'] = $prefs['offline_text'];}
    if (!(isset($camdefs[$cam]['online_text']))) {$camdefs[$cam]['online_text'] = $prefs['online_text'];}
    if (!(isset($camdefs[$cam]['filename']))) {if (isset($prefs['filename'])) {$camdefs[$cam]['filename'] = $prefs['filename'];} else {$camdefs[$cam]['filename'] = $camdefs[$cam]['filename_prepend'] . $cam . $camdefs[$cam]['filename_postpend'];}}
    if (!(isset($camdefs[$cam]['width']))) {if (isset($prefs['width'])) {$camdefs[$cam]['width'] = $prefs['width'];} else {$image_size = GetImageSize($camdefs[$cam]['filename']);$camdefs[$cam]['width'] = $image_size[0];}}
    if (!(isset($camdefs[$cam]['height']))) {if (isset($prefs['height'])) {$camdefs[$cam]['height'] = $prefs['height'];} else {$image_size = GetImageSize($camdefs[$cam]['filename']);$camdefs[$cam]['height'] = $image_size[1];}}
    if (!(isset($camdefs[$cam]['name']))) {if (isset($prefs['name'])) {$camdefs[$cam]['name'] = $prefs['name'];} else {$camdefs[$cam]['name'] = 'Webcam'.$cam;}}
  }
}

?>
